<template>
  <div style="height: 100%;background-color:white ;">
    <insert-p-k-l ref="insertPKL"/>
    <div>
      <div style="display: inline-block">
        <el-button icon="el-icon-back" type="text" @click="goBack">返回</el-button>
      </div>
      <div style="display: inline-block">|</div>
      <div style="display: inline-block;">
        <div style="padding-bottom: 15px;">
          <span style="font-family: 'Microsoft YaHei',sans-serif;display: inline-block">绑定关系</span>
        </div>
      </div>
      <div style="border-bottom: solid 1px #C9C9C9;"></div>
    </div>
    <el-row>
      <el-col :span="6" style="margin-top: 10px">
        <div>当前平台: {{ platform.name }}</div>
      </el-col>
      <el-col :span="6" style="margin-top: 5px">
        <el-button size="small" type="primary" @click="$refs.insertPKL.visible = true">新增绑定关系</el-button>
      </el-col>
    </el-row>

    <el-row>
      <div>
        <el-table highlight-current-row
                  :data="platformKnowledgeLibs.slice((currentPage-1)*pageSize,currentPage*pageSize)" border
                  style="width: 100%;margin-top: 5px" stripe :height="tableHeight">
          <el-table-column label="所属知识体系">
            <template slot-scope="scope">
              <div>{{ getSystemName(scope.row.systemType) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="knowledgeLibName"></el-table-column>
          <el-table-column :formatter="dateFormat" label="绑定时间" prop="updatedTime"></el-table-column>
          <el-table-column label="是否自动推送" width="">
            <template slot-scope="scope">
              <el-switch :disabled="!platform.status"
                         v-model="scope.row.autosyncFlag"
                         @change="updatePlatformKnowledgeLib(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <span><el-link type="danger" @click.prevent="deletePlatformKnowledgeLib(scope.row)">删除</el-link></span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-row>
    <el-row>
      <div>
        <el-pagination :total="platformKnowledgeLibs.length" :page-size="pageSize"
                       :current-page.sync="currentPage"
                       background
                       layout="total, prev, pager, next, jumper"
                       style="text-align: right;"></el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
import moment from "moment";
import InsertPKL from './insertPKL'

export default {
  components: {
    InsertPKL,
  },
  data() {
    return {
      tableHeight: '',
      platform: {
        id: '',
        name: '',
        status: false
      },
      platformKnowledgeLibs: [],
      currentPage: 1, // 第几页
      pageSize: 10
    }
  },
  methods: {
    dateFormat: function (row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format("YYYY-MM-DD  HH:mm:ss")
    },
    goBack: function () {
      this.$router.push("/platform")
    },
    updatePlatformKnowledgeLib(platformKnowledgeLib) {
      let param = new URLSearchParams();
      param.append("platformKnowledgeLib", JSON.stringify(platformKnowledgeLib))
      this.axios.post('/pmph/action/platform/updatePlatformKnowledgeLib', param)
        .then((response) => {
          console.log(response);
          platformKnowledgeLib.updatedTime = moment(new Date())
        }).catch((error) => {
        this.platformKnowledgeLibs.forEach((item) => {
          //执行代码
          if (item.id == platformKnowledgeLib.id) {
            item.status = !item.status
          }
        })
      })
    },
    deletePlatformKnowledgeLib(platformKnowledgeLib) {
      this.$confirm('是否删除？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = new URLSearchParams()
        param.append("platformKnowledgeLib", JSON.stringify(platformKnowledgeLib))
        this.axios.post('/pmph/action/platform/deletePlatformKnowledgeLib', param)
          .then((response) => {
            this.platformKnowledgeLibs.splice($.inArray(platformKnowledgeLib, this.platformKnowledgeLibs), 1)
          })
      })
    },
    getKnowledgeLibListByPlatformId() {
      this.axios.get('/pmph/action/platform/selectKnowledgeLibListByPlatformId',
        {
          params: {
            id: this.platform.id
          }
        }).then((response) => {
        this.platformKnowledgeLibs = response.data;
      }).catch((error) => {
        this.platformKnowledgeLibs = [];
        this.$message.error("未能请求到数据");
      })
    },
    getSystemName(systemId) {
      return CONST.SYSTEM_TYPE.find(item => item.value === systemId).label;
    },
  },
  created() {
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 130;
    this.platform.id = this.$route.params.id
    this.platform.name = this.$route.params.name
    this.platform.status = this.$route.params.status
    this.getKnowledgeLibListByPlatformId()
  }
}
</script>
<style scoped>
</style>
